package com.ruoyi.common.tools.excel.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.sanitationvehicle.ItemEnum;
import com.ruoyi.standingbook.sanitationvehicle.ItemImageEnum;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleImportDetailVo;
import com.ruoyi.standingbook.sanitationvehicle.service.impl.SanitationVehicleExtendProcessService;
import com.ruoyi.standingbook.v2toilet.ToiletConst;
import com.ruoyi.standingbook.v2toilet.enums.ItemStatusEnum;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class SanitationVehicleExcelListener extends MyExcelListener<SanitationVehicleImportDetailVo> {

	private String batchNo;

	public SanitationVehicleExcelListener(String batchNo) {
		this.batchNo = batchNo;
	}

	@Override
	public void doValidate(SanitationVehicleImportDetailVo data, AnalysisContext context) {
		List<String> validateList = new ArrayList<>();
		data.setBatchNo(this.batchNo);

		JSONObject excelRowData = JSONUtil.parseObj(data);

		// 判断巡检项是否为空；统计异常
		int count = 0;
		String cellValue;
		// 绑定巡检项到属性
		for (ItemEnum ti : ItemEnum.values()) {
			cellValue = excelRowData.get(ti.attr, String.class, true);
			// 转换为字典值 1，2，4
			if (ItemStatusEnum.ERR.cellValue.equalsIgnoreCase(cellValue)) {
				// 如果是 x 设置为 1
				excelRowData.set(ti.attr, ItemStatusEnum.ERR.intCode);
			} else if (ItemStatusEnum.OK.cellValue.equalsIgnoreCase(cellValue)) {
				// 如果是 √ 设置为 3
				excelRowData.set(ti.attr, ItemStatusEnum.OK.intCode);
			} else {
				// 不是√ 或× 设置为4
				excelRowData.set(ti.attr, ItemStatusEnum.NO_DATA.intCode);
			}
		}

		// 绑定巡检项图片
		for (ItemImageEnum image : ItemImageEnum.values()) {
			String ossid = this.getPicOssIdOnMap(row, image.index);
			// 只有是数字类型的值
			if (StrUtil.isNumeric(ossid)) {
				excelRowData.set(image.attr, ossid);
			}
		}

		// 把属性值绑定到对象
		data = JSONUtil.toBean(excelRowData, SanitationVehicleImportDetailVo.class);

		validData(data, validateList);

		data.setAnomalyCount(count);
		if (validateList.isEmpty()) {
			data.setProcessMsg("成功");
			data.setProcessStatus(ERecordConstant.COMMON_STATUS_SUCCESS);
			data.getDataCheckResult().setSucess(true);
			data.getDataCheckResult().setMsg("成功");
		} else {
			String errMsg = String.join(" ，", validateList);
			excelResult.getValidateList().add(errMsg);
			data.setProcessMsg(errMsg);
			data.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
			data.getDataCheckResult().setSucess(false);
			data.getDataCheckResult().setMsg(errMsg);
		}

		data.setIdx(excelResult.getList().size() / 2);
		
		String format = DateUtil.format(data.getCheckDate(), "yyyy/MM/dd");
        format = format.concat(data.getLicensePlateNo());
        //检查日期+车牌号生成ID
        data.setId(SecureUtil.md5(format));
        
		excelResult.getList().add(data);
	}

	private void validData(SanitationVehicleImportDetailVo data, List<String> validateList) {
		if (Objects.isNull(data.getCheckDate())) {
			validateList.add("[ 检查日期 ] 为空");
		}

		if (Objects.isNull(data.getLicensePlateNo())) {
			validateList.add("[ 车牌号 ] 为空");
		}

		if (Objects.isNull(data.getVehicleName())) {
			validateList.add("[ 车辆名称 ] 为空");
		}

		if (Objects.isNull(data.getProjectName())) {
			validateList.add("[ 项目名称 ] 为空");
		}

		if (Objects.isNull(data.getCheckor())) {
			validateList.add("[ 检查人 ] 为空");
		}

		if (Objects.isNull(data.getRegisterFormImage())) {
		    validateList.add("[ 登记表图片 ] 为空"); 
		}

		String noDataStatus = String.valueOf(ItemStatusEnum.NO_DATA.intCode);
		if (Objects.isNull(data.getVehiclePaintworkStatus()) || 
				 data.getVehiclePaintworkStatus().equals(noDataStatus)) {
			validateList.add("[ 整车漆面 ] 为空");
		}

		if (Objects.isNull(data.getLicensePlateStatus()) || 
				data.getLicensePlateStatus().equals(noDataStatus)) {
			validateList.add("[ 车牌清晰完整 ] 为空");
		}

		if (Objects.isNull(data.getBumperStatus()) || 
				data.getBumperStatus().equals(noDataStatus)) {
			validateList.add("[ 保险杠/安全护杠 ] 为空");
		}

		if (Objects.isNull(data.getAppearanceStatus()) || 
				data.getAppearanceStatus().equals(noDataStatus)) {
			validateList.add("[ 车容干净整洁 ] 为空");
		}

		if (Objects.isNull(data.getAirtightTransStatus()) || 
				data.getAirtightTransStatus().equals(noDataStatus)) {
			validateList.add("[ 垃圾密闭收运 ] 为空");
		}

		if (Objects.isNull(data.getCansClosedStatus()) || 
				data.getCansClosedStatus().equals(noDataStatus)) {
			validateList.add("[ 装载垃圾桶密闭 ] 为空");
		}

		if (Objects.isNull(data.getDressBehaviorStatus()) || 
				data.getDressBehaviorStatus().equals(noDataStatus)) {
			validateList.add("[ 驾驶员着装与行为规范 ] 为空");
		}

		if (Objects.isNull(data.getHornWiperStatus()) || 
				data.getHornWiperStatus().equals(noDataStatus)) {
			validateList.add("[ 喇叭、刮水器中控锁 ] 为空");
		}

		if (Objects.isNull(data.getGasPedalStatus()) || 
				data.getGasPedalStatus().equals(noDataStatus)) {
			validateList.add("[ 转向离合器变速器油门踏板制动 ] 为空");
		}

		if (Objects.isNull(data.getLubricantStatus()) || 
				data.getLubricantStatus().equals(noDataStatus)) {
			validateList.add("[ 机油刹车油变速箱油动力转向油 ] 为空");
		}

		if (Objects.isNull(data.getCoolantStatus()) || 
				data.getCoolantStatus().equals(noDataStatus)) {
			validateList.add("[ 燃油润滑油冷却液 ] 为空");
		}

		if (Objects.isNull(data.getWaterVolumeStatus()) || 
				data.getWaterVolumeStatus().equals(noDataStatus)) {
			validateList.add("[ 散热器水量 ] 为空");
		}

		if (Objects.isNull(data.getReflectingBoardStatus()) || 
				data.getReflectingBoardStatus().equals(noDataStatus)) {
			validateList.add("[ 反光板/反光锥 ] 为空");
		}

		if (Objects.isNull(data.getTripodStatus()) || 
				data.getTripodStatus().equals(noDataStatus)) {
			validateList.add("[ 三脚架/雪糕筒 ] 为空");
		}

		if (Objects.isNull(data.getSafetySignsStatus()) || 
				data.getSafetySignsStatus().equals(noDataStatus)) {
			validateList.add("[ 全车安全警示标识 ] 为空");
		}

		if (Objects.isNull(data.getReflectingMirrorStatus()) || 
				data.getReflectingMirrorStatus().equals(noDataStatus)) {
			validateList.add("[ 反光镜及盲区后视镜 ] 为空");
		}

		if (Objects.isNull(data.getExtinguishersStatus()) || 
				data.getExtinguishersStatus().equals(noDataStatus)) {
			validateList.add("[ 车载灭火器完好、有效 ] 为空");
		}

		if (Objects.isNull(data.getTiresStatus()) || 
				data.getTiresStatus().equals(noDataStatus)) {
			validateList.add("[ 轮胎有无开裂或鼓包 ] 为空");
		}

		if (Objects.isNull(data.getBrakeStatus()) || 
				data.getBrakeStatus().equals(noDataStatus)) {
			validateList.add("[ 油刹汽刹 ] 为空");
		}

		if (Objects.isNull(data.getRearviewMirrorStatus()) || 
				data.getRearviewMirrorStatus().equals(noDataStatus)) {
			validateList.add("[ 后视镜完好 ] 为空");
		}

		if (Objects.isNull(data.getSteeringWheelsStatus()) || 
				data.getSteeringWheelsStatus().equals(noDataStatus)) {
			validateList.add("[ 方向盘、仪表盘显示正常 ] 为空");
		}

		if (Objects.isNull(data.getBrakePedalStatus()) || 
				data.getBrakePedalStatus().equals(noDataStatus)) {
			validateList.add("[ 电门踏板、制动踏板正常 ] 为空");
		}

		if (Objects.isNull(data.getHornStatus()) || 
				data.getHornStatus().equals(noDataStatus)) {
			validateList.add("[ 喇叭、雨刮正常 ] 为空");
		}

		if (Objects.isNull(data.getLampStatus()) || 
				data.getLampStatus().equals(noDataStatus)) {
			validateList.add("[ 远光灯、近光灯、转向灯正常 ] 为空");
		}

		if (Objects.isNull(data.getHydraulicElevatorStatus()) || 
				data.getHydraulicElevatorStatus().equals(noDataStatus)) {
			validateList.add("[ 液压部分升降是否正常 ] 为空");
		}

		if (Objects.isNull(data.getAlcoholTestStatus()) || 
				data.getAlcoholTestStatus().equals(noDataStatus)) {
			validateList.add("[ 酒精测试结果 ] 为空");
		}

		if (Objects.isNull(data.getLicensePlateStatus1()) || 
				data.getLicensePlateStatus1().equals(noDataStatus)) {
			validateList.add("[ 车辆牌照齐全 ] 为空");
		}

		if (Objects.isNull(data.getDriverLicenseStatus()) || 
				data.getDriverLicenseStatus().equals(noDataStatus)) {
			validateList.add("[ 驾驶员证件是否有效 ] 为空");
		}

		if (Objects.isNull(data.getTripCriteriaStatus()) || 
				data.getTripCriteriaStatus().equals(noDataStatus)) {
			validateList.add("[ 是否达到出车标准 ] 为空");
		}
		
		checkData(data, validateList);
	}

	private void checkData(SanitationVehicleImportDetailVo data, List<String> validateList) {
		StringBuilder builder = new StringBuilder();
		JSONObject jsonObject = JSONUtil.parseObj(data);
		for (ItemEnum ti : ItemEnum.values()) {
			// 先设置BillItem
			// 根据excel中的 √ × 或 空值 判断状态是什么
			String attr = ti.attr;
			int status = jsonObject.get(attr, Integer.class, true);

			//String _attr = ti.attr.concat(ToiletConst.EXCEL_IMAGE_BEFORE);
			String _attr = SanitationVehicleExtendProcessService.itemNameToItemImageName(ti.attr, true);
			String imageBefore = jsonObject.getStr(_attr, "");
			if (status == 1 && !StringUtils.isNumeric(imageBefore)) {
				builder.append(ti.note + ", ");
			}
		}

		if (builder.length() != 0) {
			String temp = builder.toString();
			temp = temp.substring(0, temp.length() - 2);
			validateList.add(String.format("异常检查项 [ %s ]缺少检查前照片", temp));
		}
	}
	
	@Override
	protected void doOnExceptionProcess(String errorMsg) {
		SanitationVehicleImportDetailVo vo = new SanitationVehicleImportDetailVo();

		vo.setBatchNo(this.batchNo);
		vo.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		vo.setProcessMsg(errorMsg);
		this.excelResult.getList().add(vo);

	}
}
